package com.hyk.service.impl;

import com.baomidou.mybatisplus.extension.exceptions.ApiException;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hyk.domain.User;
import com.hyk.domain.UserBank;
import com.hyk.mapper.UserBankMapper;
import com.hyk.service.UserBankService;
import com.hyk.service.UserService;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Service
public class UserBankServiceImpl extends ServiceImpl<UserBankMapper, UserBank> implements UserBankService{

    @Resource
    private UserService userService;

    @Override
    public void addUserBank(UserBank userBank) {
        String userId = SecurityContextHolder.getContext().getAuthentication().getPrincipal().toString();
        User user = userService.getById(userId);
        //验证支付密码
        BCryptPasswordEncoder bCryptPasswordEncoder = new BCryptPasswordEncoder();
        boolean matches = bCryptPasswordEncoder.matches(userBank.getPayPassword(), user.getPaypassword());
        if (!matches) {
            throw new ApiException("支付密码错误");
        }
        //todo 验证银行卡 --》 阿里云服务

        //保存银行卡信息
        userBank.setStatus((byte)1);
        userBank.setUserId(Long.parseLong(userId));
        save(userBank);
    }

}
